<template>
  <div v-show="detailedPageVisible" class="app-container">
    <!-- PageHeader 页头 -->
    <div>
      <el-page-header @back="goBack" content="通知配置记录"/>
    </div>

    <div class="tableData">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="状态" prop="state">
          <el-select v-model="queryParams.state" clearable>
            <el-option
              v-for="dict in dict.type.notify_config_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="通知时间" prop="historyTime">
          <el-date-picker
            v-model="queryParams.historyTime"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>



    <el-table v-loading="loading" :data="notifyHistoryList">
<!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="时间" sortable align="center" prop="notifyTime"/>
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.notify_config_state" :value="scope.row.state"/>
          <el-button
            v-show="scope.row.state === 'error'"
            size="mini"
            type="text"
            icon="el-icon-list"
            @click="SelectErrorStack(scope.row)"
            v-hasPermi="['notify:config:list']"
          >查看
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-list"
            @click="handleSelectContext(scope.row)"
            v-hasPermi="['notify:config:list']"
          >查看数据
          </el-button>

        </template>
      </el-table-column>
    </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getHistoryList(notifierId)"
      />
    </div>


    <!-- 查看通知数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" class="contextForm" label-width="150px">
        <el-divider></el-divider>

        <el-input class="contextInput" v-model="form.context" type="textarea" readonly/>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-divider></el-divider>
        <el-button type="primary" @click="cancel">退 出</el-button>
      </div>
    </el-dialog>

    <!-- 查看错误信息对话框 -->
    <el-dialog :title="title" :visible.sync="errorStack" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" class="contextForm" label-width="150px">
        <el-divider></el-divider>

        <div style="width: 100%;margin-top: 21px">
          <el-input class="errorInput" :rows="17" v-model="form.errorStack" type="textarea" readonly/>
        </div>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-divider></el-divider>
        <el-button type="primary" @click="cancel">退 出</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {listHistory, getHistory} from "@/api/notify/history";
import {mapState} from 'vuex'

export default {
  name: 'Detailed',
  components: {},
  dicts: ['notify_config_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 通知配置表格数据
      notifyHistoryList: [],
      notifierId: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      errorStack: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        provider: null,
        configuration: null,
        notifierId: null,
        name: null,
        description: null,
        type: null,
        historyTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
      }
    }
  },
  computed: {
    ...mapState({
      configData: state => state.configData,
      detailedPageVisible: state => state.config.detailedPageVisible,
    }),
  },

  methods: {

    // 表单重置
    reset() {
      this.form = {
        id: null,
        provider: null,
        configuration: null,
        name: null,
        description: null,
        type: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    // 搜索条件重置
    resetQueryParams() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        provider: null,
        configuration: null,
        name: null,
        description: null,
        type: null,
        historyTime: null,
      };
      this.resetForm("form");
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    /** 查询通知配置记录列表 */
    getHistoryList(id) {

      this.queryParams.notifierId = id;
      listHistory(this.queryParams).then(response => {
        this.notifyHistoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      this.notifierId = id;
    },

    /**
     *  返回主页
     *  隐藏详情页
     */
    goBack() {
      this.resetQueryParams();
      this.$store.commit("DETAILED_CONFIG_VISIBLE", false);
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.historyTime !== null) {
        for (let i = 0; i < this.queryParams.historyTime.length; i++) {
          this.getTime(this.queryParams.historyTime[i], i);
        }
        //给时间赋值
        this.queryParams.endTime = this.queryParams.historyTime[1];
        this.queryParams.notifyTime = this.queryParams.historyTime[0];
      }

      this.getHistoryList(this.queryParams.notifierId);
    },
    /** 重置按钮操作 */
    resetQuery() {

      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 将标准时间格式转化 */
    getTime(time, i) {
      let date = new Date(time);
      let y = date.getFullYear();
      let m = date.getMonth() + 1;
      m = m < 10 ? ('0' + m) : m;
      let d = date.getDate();
      d = d < 10 ? ('0' + d) : d;
      let h = date.getHours();
      h = h < 10 ? ('0' + h) : h;
      let min = date.getMinutes();
      min = min < 10 ? ('0' + min) : min;
      let s = date.getSeconds();
      s = s < 10 ? ('0' + s) : s;
      let conversion_time1 = y + '-' + m + '-' + d + ' ' + h + ':' + min + ':'
        + s;

      return this.queryParams.historyTime[i] = conversion_time1;
    },


    /** 查看数据按钮 */
    handleSelectContext(row) {
      getHistory(row.id).then(response => {
        this.form = response.data;

        this.open = true;
        this.title = "详情";
      });
    },

    /** 查看按钮 */
    SelectErrorStack(row) {
      getHistory(row.id).then(response => {
        this.form = response.data;
        this.errorStack = true;
        this.title = "错误信息";
      });
    }

  }
}
</script>

<style scoped>

.contextInput {
  height: 100px;
}
.tableData {
  margin-top: 30px;
}
</style>
